package lab03.test13;

import java.sql.*;

public class Test {
    public static void main(String[] args) {
        /**
         * Connection
         * Statement
         * ResultSet
         */
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        String sql = "insert into t_students(real_name,age,gander,department,stu_no) " +
                "select 'hu',19,1,'CS','1606101116' UNION "+
                "select 'wang',20,0,'IS','1606101117' UNION "+
                "select 'cheng',19,1,'CS','1606101118' UNION "+
                "select 'zhao',18,0,'IS','1606101119' UNION "+
                "select 'yao',19,0,'CS','1606101120' UNION "+
                "select 'lin',19,1,'IS','1606101121' UNION "+
                "select 'wen',19,0,'S','1606101122' UNION "+
                "select 'fu',19,1,'IS','1606101123' UNION "+
                "select 'qiu',19,0,'CS','1606101124' UNION "+
                "select 'he',19,1,'CS','1606101125' ";

        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                    "root",
                    "123456"

            );

            statement = connection.createStatement();

            statement.execute(sql);
            statement.execute("DELETE FROM t_students WHERE id = 5");
            statement.execute("UPDATE t_students SET department = '信息与工程学院' WHERE id = 3");

            resultSet = statement.executeQuery("SELECT * FROM t_students");
            while(resultSet.next()){
                System.out.print(resultSet.getInt(1)+" \t");
                System.out.print(resultSet.getString(2)+" \t");
                System.out.print(resultSet.getString(3)+" \t");
                System.out.print(resultSet.getInt(4)+" \t");
                System.out.print(resultSet.getInt(5)+" \t");
                System.out.print(resultSet.getString(6));
                System.out.println();
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
